package com.a3dweijing.wetouch2.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.a3dweijing.wetouch2.Common.CallBack;
import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.adapter.CardListAdapter;
import com.a3dweijing.wetouch2.base.App;
import com.a3dweijing.wetouch2.base.BaseActivity;
import com.a3dweijing.wetouch2.bean.CardListMsg;
import com.a3dweijing.wetouch2.bean.ReferralcodeDate;
import com.a3dweijing.wetouch2.constant.Api;
import com.a3dweijing.wetouch2.datasave.Singleton;
import com.a3dweijing.wetouch2.network.Commsubscribe;
import com.a3dweijing.wetouch2.network.RetrofitUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

import static com.a3dweijing.wetouch2.Activity.AddCardActivity.ADDCARDACTIVITYCODE;

/**
 * 银行卡列表
 */

public class CardListActivity extends BaseActivity
{
    
    @BindView(R.id.back)
    ImageView back;
    
    @BindView(R.id.top_text)
    TextView topText;
    
    @BindView(R.id.top_right)
    TextView topRight;
    
    @BindView(R.id.top_all)
    RelativeLayout topAll;
    
    @BindView(R.id.progressBar)
    ProgressBar progressBar;
    
    @BindView(R.id.loading_date)
    LinearLayout loadingDate;
    
    @BindView(R.id.resaut_status_img)
    ImageView resautStatusImg;
    
    @BindView(R.id.resaut_status)
    LinearLayout resautStatus;
    
    @BindView(R.id.listview)
    ListView listview;
    
    @BindView(R.id.submit)
    LinearLayout submit;
    
    private ArrayList<CardListMsg.BankcardListBean> list;
    
    private CardListAdapter adapter;
    
    private Api api;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_binding_card_list);
        ButterKnife.bind(this);
        initView();
        initDate();
    }
    
    private void initView()
    {
        topText.setText("绑定银行卡");
        if (adapter == null)
        {
            list = new ArrayList<>();
            adapter = new CardListAdapter(this, list, R.layout.card_list_item);
            listview.setAdapter(adapter);
        }
    }
    
    private void initDate()
    {
        int id = Integer.parseInt(Singleton.getInstance().getUser().getUser_id());
        api = RetrofitUtil.getInstance().getApiOfPhp();
        Observable<CardListMsg> cData = api.getBanklist(id);
        new Commsubscribe<CardListMsg>()
            .createSubscribe(cData, loadingDate, resautStatus, disposables, this, new CallBack()
            {
                @Override
                public void doNext(int what, Object obj)
                {
                    CardListMsg cardListMobjsg = (CardListMsg)obj;
                    List<CardListMsg.BankcardListBean> listBean = cardListMobjsg.getBankcardList();
                    if (cardListMobjsg.isFlag())
                    {
                        if (listBean != null && listBean.size() > 0)
                        {
                            list.clear();
                            list.addAll(listBean);
                        }
                        else
                        {
                            resautStatus.setVisibility(View.VISIBLE);
                        }
                        adapter.notifyDataSetChanged();
                        
                    }
                }
            });
        
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ADDCARDACTIVITYCODE && resultCode == RESULT_OK)
        {
            initDate();
        }
    }
    
    @OnClick({R.id.back, R.id.submit})
    public void onViewClicked(View view)
    {
        switch (view.getId())
        {
            case R.id.back:
                finish();
                break;
            case R.id.submit:
                AddCardActivity.startActivity(this);
                break;
        }
    }
    
}
